package org.pzy.kafka;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Properties;

/**
 * @Author: DRevelation
 * @Date: 2022/3/9 13:43
 */
@Slf4j
public class OrderProducer {

    public static void main(String[] args) throws InterruptedException {
        Properties props = new Properties();
        props.put("bootstrap.servers", "192.168.56.101:9092");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("batch", 16384);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

//        props.put("partition.class", "org.pzy.kafka.KafkaPartitioner");

        KafkaProducer<String, String> producer = new KafkaProducer<>(props);
        for (int i = 0; i < 100; i++) {
            producer.send(new ProducerRecord<>("order", "订单信息!" + i), new Callback() {
                @Override
                public void onCompletion(RecordMetadata metadata, Exception exception) {
                    log.info("发送kafka消息成功: 返回{}", metadata);
                }
            });
//            producer.send(new ProducerRecord<>("order_my_partition", "my", "自定义Partition订单信息!" + i));
            Thread.sleep(100);
        }
    }

}
